﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PrintDemo.Entity;
using MySql.Data.MySqlClient;
using PrintDemo.DBUtil;

namespace PrintDemo.DAL
{
    public class ChangefulDAL
    {
        public List<ChangefulEntity> GetList()
        {
            MySqlConnection conn = null;
            MySqlDataReader reader = null;
            List<ChangefulEntity> orders = new List<ChangefulEntity>();
            try
            {
                string select = "select Id,BatchNo,BoxNum from Changeful";
                reader = SQLFactory.GetMySqlHelper().ExecuteSqlForReader(select, out conn);
                while (reader.Read())
                {
                    ChangefulEntity order = new ChangefulEntity();
                    if (!reader.IsDBNull(0))
                    {
                        order.Id = reader.GetString("Id");
                    }
                    if (!reader.IsDBNull(1))
                    {
                        order.BatchNo = reader.GetInt32("BatchNo");
                    }
                    if (!reader.IsDBNull(2))
                    {
                        order.BoxNum = reader.GetInt32("BoxNum");
                    }
                    orders.Add(order);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (conn != null)
                    conn.Close();
            }
            return orders;
        }

        public int Update(ChangefulEntity model)
        {
            try
            {
                string SqlStr = "update Changeful set BatchNo={1},BoxNum={2} where Id='{0}'";
                SqlStr = string.Format(SqlStr, new object[] { model.Id, model.BatchNo, model.BoxNum });
                SQLFactory.GetMySqlHelper().ExecuteSqlNonQuery(SqlStr);
            }
            catch (Exception ex)
            {
                return -1;
            }
            return 1;
        }
    }
}
